package de.tudarmstadt.ukp.muip.fibqdojo.rule.japanese;

import java.util.HashMap;
import java.util.List;

import com.google.common.base.Optional;

import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQRule;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentence;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentenceToken;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQToken;
import de.tudarmstadt.ukp.muip.fibqdojo.visitor.GetFormVisitor;
import de.tudarmstadt.ukp.muip.fibqdojo.visitor.GetTokenVisitor;

public class KanjiWhiteListFilterRule implements IFIBQRule {

    private HashMap<String, String> map;

    public KanjiWhiteListFilterRule(List<String> kanjiWhiteList) {
        map = new HashMap<String, String>();
        for (String kanji : kanjiWhiteList) {
            map.put(kanji, "");
        }
    }

    @Override
    public boolean match(IFIBQSentence sentence) {
        return false;
    }

    @Override
    public boolean match(IFIBQSentenceToken sentenceToken) {
        GetTokenVisitor getToken = new GetTokenVisitor();
        sentenceToken.accept(getToken);
        Optional<IFIBQToken> wrappedToken = getToken.getToken();
        if (!wrappedToken.isPresent()) {
            return false;
        }
        IFIBQToken token = wrappedToken.get();
        GetFormVisitor getForm = new GetFormVisitor();
        token.accept(getForm);
        String form = getForm.getForm();
        return !containsOnlyKanjisOnWhitelist(form);
    }

    private boolean containsOnlyKanjisOnWhitelist(String form) {
        char LOWERBOUND = '\u4E00';
        char UPPERBOUND = '\u9FBF';
        for (int i = 0; i < form.length(); i++) {
            char curChar = form.charAt(i);
            if (!(curChar >= LOWERBOUND && curChar <= UPPERBOUND)) {
                continue;
            }
            if (!map.containsKey("" + curChar)) {
                return false;
            }
        }
        return true;
    }

}
